package com.yc.http.tomcat.impl;

import com.yc.http.tomcat.Cookie;
import com.yc.http.tomcat.HttpServletRequest;
import com.yc.http.tomcat.HttpServletResponse;
import com.yc.http.tomcat.ServletException;

import java.io.IOException;
import java.io.PrintWriter;

public class ShowCookie extends HttpServletImpl{

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        // 浏览器每次发出请求, 都会带上当前路径下的cookie数据
        Cookie[] cookies = request.getCookies();
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println("<h1>Cookies数据</h1>");
        out.println("<ol>");
        for (Cookie cookie : cookies) {
            out.append("<li>")
                    .append(cookie.getName())
                    .append(" = ")
                    .append(cookie.getValue())
                    .append("</li>");
        }
        out.println("</ol>");

        out.println("<h1>添加Cookies数据</h1>");
        out.append("<form action='addCookie.s'>")
                .append("名称:<input name='name'><br>")
                .append("  值:<input name='value'><br>")
                .append("有效期:<input name='maxAge' type='number'><br>")
                .append("<button>添加Cookie</button>")
                .append("</form>");

    }
}
